cap program drop load_predicted_diag_data
program define load_predicted_diag_data
	args spec data_path mlr_suffix
	* Load in obs-level data
	use "`data_path'/predicted_prem_4197169.dta", clear
	* Encode 
	encode PAYER_ID, gen(payer_code)
	
	keep if x_av >0  
	
	rename PAYER_ID payer_id
	merge m:1 payer_id year using "$WORK/../../data/orig/carrier_collapsed_mlr`mlr_suffix'.dta"
	keep if _merge == 3
	drop _merge
	
	merge m:1 payer_id year using "$data_path/outstate_instruments.dta"
	
	bysort payer_id year: egen payeryear_sum_s_ij = sum(sum_s_ij)
	gen s_a_jt = (admin_sum / 12) * (sum_s_ij / payeryear_sum_s_ij)  / 100
	gen s_sec3_jt = (sec3_sum / 12) * (sum_s_ij / payeryear_sum_s_ij)  / 100
	gen s_sec4_jt = (sec4_sum / 12) * (sum_s_ij / payeryear_sum_s_ij)  / 100
	gen s_sec5_jt = (sec5_sum / 12) * (sum_s_ij / payeryear_sum_s_ij)  / 100
end

cap program drop load_pred_diag_data_bootstrap
program define load_pred_diag_data_bootstrap
	args data_path_bootstrap mlr_suffix
	* Load in obs-level data
	use "`data_path_bootstrap'", clear
	* Encode 
	encode PAYER_ID, gen(payer_code)
	
	keep if x_av >0  
	
	rename PAYER_ID payer_id
	merge m:1 payer_id year using "$WORK/../../data/orig/carrier_collapsed_mlr`mlr_suffix'.dta"
	keep if _merge == 3
	drop _merge
	
	merge m:1 payer_id year using "$data_path/outstate_instruments.dta"
	
	bysort payer_id year: egen payeryear_sum_s_ij = sum(sum_s_ij)
	gen s_a_jt = (admin_sum / 12) * (sum_s_ij / payeryear_sum_s_ij)  / 100
	gen s_sec3_jt = (sec3_sum / 12) * (sum_s_ij / payeryear_sum_s_ij)  / 100
	gen s_sec4_jt = (sec4_sum / 12) * (sum_s_ij / payeryear_sum_s_ij)  / 100
	gen s_sec5_jt = (sec5_sum / 12) * (sum_s_ij / payeryear_sum_s_ij)  / 100
end

cap program drop gen_oos_instruments
program define gen_oos_instruments
	use "$WORK/../../data/orig/carrier_collapsed_mlr_allstates.dta", clear
	keep all_subs indiv_subs value1_YEARLY admin_mmean payer_id year oregon
	reshape wide all_subs indiv_subs value1_YEARLY admin_mmean, i(payer_id year) j(oregon)

	save "$data_path/outstate_instruments.dta", replace
end

cap program drop set_grstyle
program define set_grstyle
	grstyle init
	grstyle set plain
end
